<template>
  <transition name="fade">
    <div class="notice" v-if="show" :style="setStyle">
      <div class="main">
        <div class="delete" v-if="!options.autoClose" @click="close">
          <i class="iconfont icon-cuowutishixiao"></i>
        </div>
        <div class="content">{{options.content}}</div>
      </div>
    </div>
  </transition>
</template>

<script type="text/ecmascript-6">
  export default {
    props: {
      options: {
        type: Object,
        default: () => {
          return {};
        }
      },
      show: {
        type: Boolean,
        default: true
      }
    },
    data () {
      return {
        timers: []
      };
    },
    computed: {
      setStyle () {
        return {
          color: this.options.textColor || '#fff',
          background: this.options.backgroundColor || '#21e7b6'
        };
      }
    },
    methods: {
      close () {
        this.show = false;
        this.options = {};
      },
      countdown () {
        if (this.options.autoClose) {
          const t = setTimeout(() => {
            this.close();
          }, this.options.showTime || 3000);
          this.timers.push(t);
        }
      }
    },
    watch: {
      options () {
        this.timers.forEach((timer) => {
          window.clearTimeout(timer);
        });
        this.timers = [];
        this.countdown();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .notice{
    position: absolute;
    top:0;
    width: 100%;
    font-size: 18px;
    padding: 20px;

    .delete{
      position: absolute;
      top:0;
      right:0;

      i{
        font-size: 20px;
      }
    }
  }
</style>
